package com.ding.dailyup.service;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ding.dailyup.model.SysNotice;
import com.ding.dailyup.repository.SystemNoticeRepository;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;

@Service
public class SystemNoticeService {

	@Autowired
	private SystemNoticeRepository systemNoticeRepository;

	public String getSystemNotice() {
		LocalDateTime now = LocalDateTime.now();
		List<String> collect = systemNoticeRepository.findByStatus(1)
				.orElseGet(Lists::newArrayList).stream()
				.filter(sysNotice -> sysNotice.getStartTime().isBefore(now)
						&& sysNotice.getEndTime().isAfter(now))
				.sorted(Comparator.comparing(SysNotice::getSortKey)
						.thenComparing(SysNotice::getCreateTime))
				.map(SysNotice::getContent)
				.collect(Collectors.toList());
		return Joiner.on("                        ").join(collect);
	}

}
